package com.houxiurong.dao;

import com.houxiurong.entity.Blog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * BlogDao
 *
 * @author houxiurong
 */
public interface BlogDao extends JpaRepository<Blog, Integer> {

    /**
     * 获取文章存档信息
     *
     * @return
     */
    @Query("select release_date as releaseDateStr, count(id) as blogReleaseCount " +
            "from Blog " +
            "group by release_date " +
            "order by release_date desc ")
    List<Blog> getBlogArchive();


    @Query("from Blog where id=:id")
    Blog getBlogTypeById(@Param("id") Integer id);

    /**
     * 获取博客文章主体
     *
     * @return
     */
    @Query("select t1.*,t2.type_name,t1.release_date as releaseDateBlog " +
            "from Blog t1,BlogType t2 where t1.type_id = t2.id order by t1.id desc")
    List<Blog> getBlogArticle();

    /**
     * 按查询参数查询博客
     *
     * @param searchParam
     * @return
     */
    @Query("select t1.*,t2.type_name,dt1.release_date as releaseDateBlog " +
            "from Blog t1,BlogType t2 " +
            "where t1.type_id = t2.id " +
            "and (t1.title like concat('%',:searchParam,'%') or t1.summary " +
            "like concat('%',:searchParam,'%')) " +
            "order by t1.release_date desc")
    List<Blog> searchBlog(@Param("searchParam") String searchParam);


}














